package tenantchannellogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpiListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpiListLogic {
	return &UpiListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpiListLogic) UpiList(in *rpc.UpiListRequest) (*rpc.UpiListResponse, error) {
	var CommonResp rpc.CommonResp

	upiModel := model.NewTenantChannelUpiModel(l.ctx, l.svcCtx.Db)
	upiList, err := upiModel.GetAllUpi(in.Upi)
	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Database operation error"
		return &rpc.UpiListResponse{
			CommonResp: &CommonResp,
		}, err
	}

	var records []*rpc.Upi
	for _, upi := range upiList {
		records = append(records, &rpc.Upi{
			Id:           upi.ID,
			CreatedAt:    upi.CreatedAt,
			UpdatedAt:    upi.UpdatedAt,
			Upi:          upi.Upi,
			MinAmount:    upi.MinAmount,
			MaxAmount:    upi.MaxAmount,
			DailyTimes:   upi.DailyTimes,
			DailyAmount:  upi.DailyAmount,
			RemainTimes:  upi.RemainTimes,
			RemainAmount: upi.RemainAmount,
			VisionLevel:  upi.VisionLevel,
			IsDisplay:    upi.IsDisplay,
		})
	}

	CommonResp.Message = "Success"
	return &rpc.UpiListResponse{
		Records:    records,
		CommonResp: &CommonResp,
	}, nil
}
